package com.ruoyi.shangpinxinxi.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-17
 */
@Controller
@RequestMapping("/shangpinxinxi/shangpinxinxiTest")
public class ShangpinxinxiController extends BaseController
{
    private String prefix = "shangpinxinxi/shangpinxinxiTest";

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:view")
    @GetMapping()
    public String shangpinxinxiTest()
    {
        return prefix + "/shangpinxinxiTest";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Shangpinxinxi shangpinxinxi)
    {
        startPage();
        List<Shangpinxinxi> list = shangpinxinxiService.selectShangpinxinxiList(shangpinxinxi);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Shangpinxinxi shangpinxinxi)
    {
        List<Shangpinxinxi> list = shangpinxinxiService.selectShangpinxinxiList(shangpinxinxi);
        ExcelUtil<Shangpinxinxi> util = new ExcelUtil<Shangpinxinxi>(Shangpinxinxi.class);
        return util.exportExcel(list, "商品信息数据");
    }

    /**
     * 新增商品信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Shangpinxinxi shangpinxinxi) throws NoSuchAlgorithmException {
        tiaomaSet(shangpinxinxi);

        if (shangpinxinxi.getShangpinLeibie().contains("防火锁")
                || shangpinxinxi.getShangpinLeibie().contains("闭门器")
                || shangpinxinxi.getShangpinLeibie().contains("拉爆盖")) {
            shangpinxinxi.setGuiGeGao(null);
            shangpinxinxi.setGuiGeKuan(null);
            shangpinxinxi.setKaiXiang(null);
            return toAjax(shangpinxinxiService.insertShangpinxinxi(shangpinxinxi));
        }
        if (shangpinxinxi.getGuiGeKuan().equals("")
                || shangpinxinxi.getGuiGeGao().equals("")
                || shangpinxinxi.getKaiXiang().equals("")) {
            return toAjax("未填写规格尺寸或开向");
        }
        return toAjax(shangpinxinxiService.insertShangpinxinxi(shangpinxinxi));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiById(id);
        mmap.put("shangpinxinxi", shangpinxinxi);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品信息
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Shangpinxinxi shangpinxinxi) throws NoSuchAlgorithmException {
        if (shangpinxinxi.getTiaoma() != null) {
            tiaomaSet(shangpinxinxi);
        }
        return toAjax(shangpinxinxiService.updateShangpinxinxi(shangpinxinxi));
    }

    private static void tiaomaSet(Shangpinxinxi shangpinxinxi) throws NoSuchAlgorithmException {
        String tiaoma = tiaoMaAuto.QRCote(shangpinxinxi);
        shangpinxinxi.setTiaoma(tiaoma);
        String contentUnicode = tiaoMaAuto.bianhaoSet(shangpinxinxi);
        shangpinxinxi.setShangpinBianhao(contentUnicode);
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("shangpinxinxi:shangpinxinxiTest:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shangpinxinxiService.deleteShangpinxinxiByIds(ids));
    }
}
